# encoding: utf-8
# @author: luzhangdan
# @file: excel_tract.py
# @time: 2021/10/19 15:03


import pandas
from common.setting import DIR_NAME
from common.logger import GetLogger

logger = GetLogger().get_logger()


class ExcelOperator:
    def __init__(self, file):
        self.data_path = DIR_NAME + '/data/' + file
        logger.info(f"测试用例的excel路径是：{self.data_path}")

    def read_excel(self, sheet_name):
        """
        单个用例的sheet页面用例
        :param sheet_name:  sheet页面的名称
        :return:
        """
        result = pandas.read_excel(self.data_path, sheet_name, engine="openpyxl", keep_default_na=False)
        # 获取总行数, 不会算上表头
        n_rows = result.shape[0]
        logger.info(f"excel的总行数是：{n_rows}")

        # 获取总列数
        n_cols = result.columns.size
        logger.info(f"总excel的列数是：{n_cols}")

        data = []
        for row in range(n_rows):
            # 每行数据，用一个list去存储
            line = []
            for col in range(n_cols):
                # 通过位置，获取单元格的内容
                text = result.iloc[row, col]
                line.append(text)
            data.append(line)
        return data

    def case_list(self, sheet_name):
        """
        测试用例集合
        :param sheet_name:  sheet页面的名称
        :return:
        """
        result = pandas.read_excel(self.data_path, sheet_name, engine="openpyxl", keep_default_na=False)
        # 获取总行数, 不会算上表头
        n_rows = result.shape[0]
        logger.info(f"excel的总行数是：{n_rows}")

        # 获取总列数
        n_cols = result.columns.size
        logger.info(f"总excel的列数是：{n_cols}")

        data = []
        for row in range(n_rows):
            # 每行数据，用一个list去存储
            line = []
            for col in range(n_cols):
                # 通过位置，获取单元格的内容
                text = result.iloc[row, col]
                line.append(text)
            # 判断是否执行用例
            if "y" in line:
                data.append(line)
        logger.info(f"用例集合的data:{data}")
        return data


if __name__ == '__main__':
    ex_op = ExcelOperator("manage_api.xlsx")
    print(ex_op.read_excel("渠道商视图"))
